package com.blueocean_health.care.management.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.GroupPostSubsidy;
import com.blueocean_health.care.management.domain.dto.ManyToManyPercentagesDto;
import com.blueocean_health.care.management.domain.dto.NurseFeePercentagesDto;
import com.blueocean_health.care.management.domain.dto.NurseGroupInfo;
import com.blueocean_health.care.management.domain.dto.NurseGroupPercentagesDto;
import com.blueocean_health.care.management.domain.dto.TemporaryNurseDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.NurseFeeTypeEnum;
import com.blueocean_health.care.management.mapper.NurseGroupPercentagesMapper;
import com.blueocean_health.care.management.service.CheckWorkService;
import com.blueocean_health.care.management.service.ManyToManyPercentagesService;
/**
 * 多对多提成报表  2018年5月24日14:42:46
 * @author wuyanli
 *
 */
@Service
public class ManyToManyPercentagesServiceImpl implements ManyToManyPercentagesService {
	
	@Autowired
	private NurseGroupPercentagesMapper percentDao;
	
	@Resource
	private CheckWorkService workService;
	
//	private DecimalFormat df = new DecimalFormat("0.00");
	/**
	 * 多对多提成报表
	 */
	@Override
	public SysResult manyToManyTable(Map<String, String> map) {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", courrentUser.getHospitalId());
		String startTime = map.get("startTime");
		String endTime = map.get("endTime");
		int pageSize = Integer.parseInt(map.get("pageSize"));
		int page = Integer.parseInt(map.get("page"));
		//实提护理费总计
		double realPercentFeeCount = 0.00;
		//出勤天数总计
		int attendanceCountDays = 0;
//		Date startTime1 = null;
//		try {
//			startTime1 = new SimpleDateFormat("yyyy-MM-dd").parse(startTime );
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		//获取当前月份天数
		Calendar c = Calendar.getInstance();
//		c.setTime(startTime);
		String[] split = startTime.split("-");
	    c.set(Integer.parseInt(split[0]),Integer.parseInt(split[1]),0); //输入类型为int类型  
	    int nowMonthDays = c.get(Calendar.DAY_OF_MONTH);  
		//多对多返回对象实体
		List<ManyToManyPercentagesDto> manyToManyList = new ArrayList<>();
		//小组当月应收金额
		Map<String, Double> nurseFee = getNurseFee(map);
		List<NurseGroupPercentagesDto> percentagesList = percentDao.percentagesList(map);
		
		//所有小组岗位薪资总和
		List<GroupPostSubsidy> subsidyCountByGroup = percentDao.getPostSubsidyCountByGroup();
		 Map<String, Double> collect = subsidyCountByGroup.stream().collect(Collectors.toMap(GroupPostSubsidy::getGroupId,GroupPostSubsidy::getPostSubsidyCount));
		//每个护工的实出勤天数
		Map<String, Long> attendMap = workService.queryAllByStartAndEndTime(startTime, endTime);
		Map<String,String> groupMap1 = new HashMap<>();
		groupMap1.put("hospitalId", courrentUser.getHospitalId());
		List<NurseGroupPercentagesDto> percentagesList1 = percentDao.percentagesList(groupMap1);
		//每个小组参与提成后实出勤天数
		Map<String, Integer> groupActualDays = getGroupActualDays(map,percentagesList1);
		
		//临时护工对应的基本薪资 key： 小组Id value：基本薪资
		Map<String,Double> tempNurseFee = new HashMap<>();
		//所有临时护工信息
		List<TemporaryNurseDto> temporaryNurseInfo = percentDao.temporaryNurseInfo();
		for (TemporaryNurseDto temporaryNurseDto : temporaryNurseInfo) {
			//应出勤天数
			int shouldAttendDay = nowMonthDays - temporaryNurseDto.getVacationDay();
			//该护工在改小组出勤天数
			Integer day = workService.queryByWorkIdAndGroupId(temporaryNurseDto.getNurseId(), temporaryNurseDto.getGroupId(), startTime, endTime);
			Double tempNurseFees = temporaryNurseDto.getBaseSalary()/shouldAttendDay*day;
			tempNurseFee.put(temporaryNurseDto.getGroupId(), tempNurseFees);
		}
		
		for (NurseGroupPercentagesDto nurseGroupPercentagesDto : percentagesList) {
			ManyToManyPercentagesDto manyDto = new ManyToManyPercentagesDto();
			manyDto.setNurseId(nurseGroupPercentagesDto.getNurseId());
			manyDto.setWorkerName(nurseGroupPercentagesDto.getWorkerName());
			manyDto.setGroupId(nurseGroupPercentagesDto.getGroupId());
			manyDto.setGroupName(nurseGroupPercentagesDto.getGroupName());
			manyDto.setBankCard(nurseGroupPercentagesDto.getBankCard());
			manyDto.setBankFullName(nurseGroupPercentagesDto.getBankFullName());
			manyDto.setBankName(nurseGroupPercentagesDto.getBankName());
			//岗位补贴
			manyDto.setPostSubsidy(Double.valueOf(String.format("%.2f",nurseGroupPercentagesDto.getPostSubsidy())));
			//应出勤天数=当月天数-休假天数
			int shouldAttendance = nowMonthDays-nurseGroupPercentagesDto.getVacationDay();
			if(shouldAttendance <= 0){
				return SysResult.error("休假天数大于当月天数");
			}
			//实出勤天数
			int actualAttendance = 0;
			if(!attendMap.isEmpty()){
				if(attendMap.get(nurseGroupPercentagesDto.getNurseId()) != null){
					actualAttendance = new Long(attendMap.get(nurseGroupPercentagesDto.getNurseId())).intValue(); 
					
				}
			}
			
			
			//小组护理费提成
			double nurseFeePercent = 0.00;
			//改护工所有待过的小组
			List<NurseGroupInfo> nurseGroupInfo = percentDao.nurseGroupInfo(nurseGroupPercentagesDto.getNurseId());
			for (NurseGroupInfo nurseGroupInfo2 : nurseGroupInfo) {
				
				if(NurseFeeTypeEnum._1.getKey().equals(nurseGroupInfo2.getNurseFeeType())){
					//固定底薪模型  ：护理费提成为 0
					manyDto.setNurseFeePercentages(nurseFeePercent);
				}else{
					//参与提成后在该小组的实出勤天数
					int attendanceDay = 0;
					Date participationDate = null;
					Date startTime2 = null;
					
					//是否参与提成  1：是  0：否
					if("1".equals(nurseGroupPercentagesDto.getHavePercentages()) && "1".equals(nurseGroupPercentagesDto.getTemporaryNurse())){
						participationDate = TimeUtils.getDateByString(nurseGroupPercentagesDto.getParticipationDate(), TimeType.yyyy_MM_dd_HH_mm_ss);
						startTime2 = TimeUtils.getDateByString(map.get("startTime"), TimeType.yyyy_MM_dd);
						//判断开始时间是否大于开始参与提成时间
						if(participationDate.compareTo(startTime2)<0){
							attendanceDay = workService.queryByWorkIdAndGroupId(nurseGroupPercentagesDto.getNurseId(),nurseGroupInfo2.getGroupId(),map.get("startTime"), map.get("endTime")).intValue();
						}else{
							String stringByDate = TimeUtils.getStringByDate(participationDate, TimeType.yyyy_MM_dd);
							attendanceDay = workService.queryByWorkIdAndGroupId(nurseGroupPercentagesDto.getNurseId(),nurseGroupInfo2.getGroupId(),stringByDate, map.get("endTime")).intValue();
						}
						Integer groupDays = groupActualDays.get(nurseGroupInfo2.getGroupId());
						if(StringUtils.isEmpty(groupDays)){
							groupDays = 0;
						}
//						
//						int groupDays = 0;
//						if(participationDate.compareTo(startTime1)<0){
//							if(!StringUtils.isEmpty(nurseGroupPercentagesDto.getGroupId())){
//								groupDays = workService.queryByGroupId(Integer.parseInt(nurseGroupInfo2.getGroupId()),map.get("startTime"), map.get("endTime")).intValue();
//								
//							}
//						}else{
//							String stringByDate = TimeUtils.getStringByDate(participationDate, TimeType.yyyy_MM_dd);
//							if(!StringUtils.isEmpty(nurseGroupPercentagesDto.getGroupId())){
//								groupDays = workService.queryByGroupId(Integer.parseInt(nurseGroupInfo2.getGroupId()),stringByDate, map.get("endTime")).intValue();
//								
//							}
//						}
						//当前小组应收金额
						Double nurseFees = nurseFee.get(nurseGroupInfo2.getGroupId());
						if(nurseFees == null){
							nurseFees = 0.00;
						}
						
						if(groupDays != 0){
							Double postSubsidy = 0.00;
							if(!collect.isEmpty() && collect.size() != 0){
								if(!StringUtils.isEmpty(collect.get(nurseGroupInfo2.getGroupId()))){
									postSubsidy = collect.get(nurseGroupInfo2.getGroupId());
									
								}
								
							}
							//临时护工工资
							Double tempFee = tempNurseFee.get(nurseGroupInfo2.getGroupId());
							if(StringUtils.isEmpty(tempFee)){
								tempFee = 0.00;
							}
							
							//小组护理费提成 = （当月应收金额*提成比例 - 所有人的岗位工资-临时护工工资）/小组所有成员实出勤天数  
							nurseFeePercent += (nurseFees*nurseGroupInfo2.getProportions()/100-postSubsidy-tempFee)/groupDays*attendanceDay;
							
						}
						//当月应收金额
						manyDto.setNurseFees(nurseFees);

					}else{
						//未参与提成后个人护理费提成
						nurseFeePercent += 0.00;
					}
				}
			}
			
			//参与提成后个人护理费提成
			manyDto.setNurseFeePercentages(Double.valueOf(String.format("%.2f",nurseFeePercent)));
			//计算得出的基本薪资=基本薪资/应出勤天数*实出勤天数
			double computeBaseSalary = nurseGroupPercentagesDto.getBaseSalary()/shouldAttendance*actualAttendance;
			double baseSalary = 0.00;
			if(NurseFeeTypeEnum._1.getKey().equals(nurseGroupPercentagesDto.getNurseFeeType())){
				
				//固定底薪模型  基本薪资为实际计算结果
				baseSalary =  computeBaseSalary ;
				//固定薪资无保底薪资  显示为0
				manyDto.setPayroll(0.00);
				
			}else{
				//保底薪资
				manyDto.setPayroll(Double.valueOf(String.format("%.2f",nurseGroupPercentagesDto.getPayroll())));
				//提成比例模型 若实际出勤天数 > 应出勤天数，基本薪资为基本薪资
				baseSalary = actualAttendance > shouldAttendance ?  nurseGroupPercentagesDto.getBaseSalary():computeBaseSalary;
			}
			
			//实出勤天数
			manyDto.setAttendanceDay(actualAttendance);
			//基本薪资
			manyDto.setBaseSalary(Double.valueOf(String.format("%.2f",baseSalary)));
			//岗位补贴
			manyDto.setPostSubsidy(Double.valueOf(String.format("%.2f",nurseGroupPercentagesDto.getPostSubsidy())));
			//小组护理费提成
//			Map<String, Double> nurseFeeMap = getNurseFeePercentages(nurseGroupPercentagesDto,map);
			
			
		
			//应提护理费 = 基本薪资+护理费提成+岗位补贴 
			double shouldPercentFee = baseSalary + manyDto.getNurseFeePercentages() + nurseGroupPercentagesDto.getPostSubsidy();
			manyDto.setShouldPercentFee(Double.valueOf(String.format("%.2f",shouldPercentFee)));
			//提成比例为无保底薪资
//			if("0".equals(nurseGroupPercentagesDto.getHavePayroll())){
//				//实提护理费金额 = 应提护理费金额
//				manyDto.setRealPercentFee(Double.valueOf(df.format(shouldPercentFee)));
//			}else{
			//实提护理费金额 = 应提护理费金额  >= 保底薪资 ？ 应提护理费金额 ： 保底/应出勤*实出勤+岗位补贴
			double percentFee = nurseGroupPercentagesDto.getPayroll()/shouldAttendance*actualAttendance + manyDto.getPostSubsidy();
			double realPercentFee = manyDto.getShouldPercentFee() >= manyDto.getPayroll() ? manyDto.getShouldPercentFee() : percentFee;
			manyDto.setRealPercentFee(Double.valueOf(String.format("%.2f",realPercentFee)));
//			}
			//实提护理费总计
			realPercentFeeCount += manyDto.getRealPercentFee();
			//出勤天数总计
			attendanceCountDays += manyDto.getAttendanceDay();
			//将多对多字段封装
			manyToManyList.add(manyDto);
		}
		
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> pageMap = new HashMap<>();
		List<ManyToManyPercentagesDto> manyByPageList = new ArrayList<>();
		//手动分页
		int total = percentagesList.size();
		int beginSize = (page-1)*pageSize ;
		if(beginSize >= total){
			resultMap.put("list", manyByPageList);
			pageMap.put("total", total);
			resultMap.put("realPercentFeeCount", String.format("%.2f",realPercentFeeCount));
			resultMap.put("attendanceCountDays", attendanceCountDays);
			resultMap.put("pageInfo", pageMap);
			return SysResult.ok(resultMap);
		}
		int endSize = page*pageSize > total ? total : page*pageSize;
		for (int i = beginSize; i < endSize; i++) {
			manyByPageList.add(manyToManyList.get(i));
		}
		//手动分页结果
		resultMap.put("list", manyByPageList);
		pageMap.put("total", total);
		resultMap.put("realPercentFeeCount", String.format("%.2f",realPercentFeeCount));
		resultMap.put("attendanceCountDays", attendanceCountDays);
		resultMap.put("pageInfo", pageMap);
		return SysResult.ok(resultMap);
	}
	/**
	 * 每个小组参与提成后实际出勤天数
	 * @return
	 */
	public Map<String,Integer> getGroupActualDays(Map<String,String> map,List<NurseGroupPercentagesDto> percentagesList){
		Map<String,Integer> groupMap = new HashMap<>();
		Date participationDate = null;
		Date startTime = null;
		for (NurseGroupPercentagesDto nurseGroupPercentagesDto : percentagesList) {
			
			List<NurseGroupInfo> nurseGroupInfo = percentDao.nurseGroupInfo(nurseGroupPercentagesDto.getNurseId());
			for (NurseGroupInfo nurseGroupInfo2 : nurseGroupInfo) {
				
				int attendanceDay = 0;
				if("1".equals(nurseGroupPercentagesDto.getHavePercentages())){
					
					participationDate = TimeUtils.getDateByString(nurseGroupPercentagesDto.getParticipationDate(), TimeType.yyyy_MM_dd_HH_mm_ss);
					startTime = TimeUtils.getDateByString(map.get("startTime"), TimeType.yyyy_MM_dd);
					//判断开始时间是否大于开始参与提成时间
					if(participationDate.compareTo(startTime)<0){
						attendanceDay = workService.queryByWorkIdAndGroupId(nurseGroupPercentagesDto.getNurseId(),nurseGroupInfo2.getGroupId(),map.get("startTime"), map.get("endTime")).intValue();
					}else{
						String stringByDate = TimeUtils.getStringByDate(participationDate, TimeType.yyyy_MM_dd);
						attendanceDay = workService.queryByWorkIdAndGroupId(nurseGroupPercentagesDto.getNurseId(),nurseGroupInfo2.getGroupId(),stringByDate, map.get("endTime")).intValue();
					}
					
				}
				Integer groupDays = groupMap.get(nurseGroupInfo2.getGroupId());
				if(StringUtils.isEmpty(groupDays)){
					groupMap.put(nurseGroupInfo2.getGroupId(),attendanceDay);
				}else{
					groupMap.put(nurseGroupInfo2.getGroupId(),groupDays+attendanceDay);
				}
			}
		}
		
		return groupMap;
	}
	
	public Map<String,Double> getNurseFee(Map<String,String> map){
		//每个订单当月应收金额列表
		List<NurseFeePercentagesDto> nurseFeeList = percentDao.nurseFeeList(map);
		
		String startTimeStr = map.get("startTime");
		String endTimeStr = map.get("endTime");
		Date monthStartTime = TimeUtils.str2Date(startTimeStr+" 00:00:00");
		Date monthEndTime = TimeUtils.str2Date(endTimeStr+" 00:00:00");
		//key:groupId  value:小组当月应收金额
		Map<String,Double> feeMap = new HashMap<>();
		//小组当月应收金额
		for (NurseFeePercentagesDto nurseFeePercentagesDto : nurseFeeList) {
			Integer chaiDay = nurseFeePercentagesDto.getCaiDay();
			Integer chaiHour = nurseFeePercentagesDto.getCaiHour();
			Double actualDayPrice = nurseFeePercentagesDto.getActualDayPrice();
			Double actualTotalPrice = nurseFeePercentagesDto.getActualTotalPrice();
			Integer noMonthDay = nurseFeePercentagesDto.getNoMonthDay();
			Integer noMonthHour = nurseFeePercentagesDto.getNoMonthHour();
			Double oneHourPrice = nurseFeePercentagesDto.getOneHourPrice();
			Double nurseFee = 0.00;
			String beginTimesStr = nurseFeePercentagesDto.getBeginTimes();
			String endTimesStr = nurseFeePercentagesDto.getEndTimes();
			Date orderBeginTime = TimeUtils.str2Date(beginTimesStr+" 00:00:00");
			Date orderEndTime = TimeUtils.str2Date(endTimesStr+" 00:00:00");
			//1.0.8 费用用减法计算  2018年9月3日11:26:18
			//若查询月份，包含订单的开始时间，不包含结束时间，当月的收款金额=实际日服务费*天数+实际小时服务费*小时数
			if((monthEndTime.getTime() >= orderBeginTime.getTime() 
					&& monthEndTime.getTime() < orderEndTime.getTime())
					|| (monthStartTime.getTime() > orderBeginTime.getTime() 
							&& monthEndTime.getTime() < orderEndTime.getTime())){
				nurseFee = chaiDay*actualDayPrice+chaiHour*oneHourPrice;
			//若查询月份，包含订单开始和结束时间，当月的收款金额直接取订单中存储的实际费用
			}else if(monthStartTime.getTime() <= orderBeginTime.getTime() 
					&& monthEndTime.getTime() >= orderEndTime.getTime()){
				nurseFee = actualTotalPrice;
			/*若查询月份，包含订单结束时间，不包含开始时间，
				当月的收款费用=实际费用-（实际日服务费*未在查询范围天数+实际小时服务费*小时数）*/
			}else if(monthStartTime.getTime() >= orderBeginTime.getTime() 
					&& monthStartTime.getTime() < orderEndTime.getTime()){
				Double noMonthPrice = noMonthDay*actualDayPrice+noMonthHour*oneHourPrice;	
				nurseFee = actualTotalPrice - noMonthPrice;
			}
			
			
			if(!feeMap.containsKey(nurseFeePercentagesDto.getGroupId())){
				feeMap.put(nurseFeePercentagesDto.getGroupId(), nurseFee);
			}else{
				Double fee = feeMap.get(nurseFeePercentagesDto.getGroupId());
				feeMap.put(nurseFeePercentagesDto.getGroupId(), nurseFee+fee);
			}
		}
		
		return feeMap;
	}
	/**
	 * 小组护理费提成  2018年5月30日10:12:51
	 * @param percentDto
	 * @param startTime
	 * @param endTime
	 * @return key:groupId  value:小组所有成员出勤天数
	 */
	/*public Map<String,Double> getNurseFeePercentages(NurseGroupPercentagesDto percentDto,Map<String,String> map){
		//每个订单当月应收金额列表
		List<NurseFeePercentagesDto> nurseFeeList = percentDao.nurseFeeList(map);
		//key:groupId  value:小组当月应收金额
		Map<String,Double> feeMap = new HashMap<>();
		//小组当月应收金额
		for (NurseFeePercentagesDto nurseFeePercentagesDto : nurseFeeList) {
			Integer chaiDay = nurseFeePercentagesDto.getCaiDay();
			Integer chaiHour = nurseFeePercentagesDto.getCaiHour();
			Double actualDayPrice = nurseFeePercentagesDto.getActualDayPrice();
			Double oneHourPrice = nurseFeePercentagesDto.getOneHourPrice();
			Double nurseFee = chaiDay*actualDayPrice+chaiHour*oneHourPrice;
			
			if(!feeMap.containsKey(nurseFeePercentagesDto.getGroupId())){
				feeMap.put(nurseFeePercentagesDto.getGroupId(), nurseFee);
			}else{
				Double fee = feeMap.get(nurseFeePercentagesDto.getGroupId());
				feeMap.put(nurseFeePercentagesDto.getGroupId(), nurseFee+fee);
			}
		}
		// key:groupId value:小组所有成员出勤天数
		Date participationDate = TimeUtils.getDateByString(percentDto.getParticipationDate(), TimeType.yyyy_MM_dd_HH_mm_ss);
		Date startTime = TimeUtils.getDateByString(map.get("startTime"), TimeType.yyyy_MM_dd);
		Map<Integer, Long> attendanceMap = new HashMap<>();
		if(participationDate.compareTo(startTime)<0){
			attendanceMap = workService.queryAllByGroupId(map.get("startTime"), map.get("endTime"));
		}else{
			String stringByDate = TimeUtils.getStringByDate(participationDate, TimeType.yyyy_MM_dd);
			attendanceMap = workService.queryAllByGroupId(stringByDate, map.get("endTime"));
		}
		// key:groupId value:小组护理费提成
		Map<String,Double> percentMap = new HashMap<>(); 
		Iterator<Map.Entry<Integer, Long>> entries = attendanceMap.entrySet().iterator();  
		while (entries.hasNext()) {  
		  
		    Map.Entry<Integer, Long> entry = entries.next();  
		    if(percentDto.getGroupId().equals(entry.getKey()+"")){
		    	//小组当月应收金额
		    	Double nurseFee = feeMap.get(percentDto.getGroupId());
		    	if(nurseFee == null){
		    		nurseFee = 0.00;
		    	}
		    	//小组护理费提成 = （当月应收金额*提成比例 - 岗位工资）/小组所有成员实出勤天数
		    	double percent = (nurseFee*percentDto.getProportions()/100-percentDto.getPostSubsidy())/entry.getValue();
		    	
		    	percentMap.put(percentDto.getGroupId(), percent);
		    	break;
		    }
		}  
		
		return percentMap;
	}*/
	/**
	 * 导出excel  2018年6月1日16:40:40
	 */
	@Override
	public List<ManyToManyPercentagesDto> getPercentagesExcel(Map<String, String> map) {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", courrentUser.getHospitalId());
		String startTime = map.get("startTime");
		String endTime = map.get("endTime");
		//获取当前月份天数
		Calendar c = Calendar.getInstance();
//		c.setTime(startTime);
		String[] split = startTime.split("-");
	    c.set(Integer.parseInt(split[0]),Integer.parseInt(split[1]),0); //输入类型为int类型  
	    int nowMonthDays = c.get(Calendar.DAY_OF_MONTH);  
		//多对多返回对象实体
		List<ManyToManyPercentagesDto> manyToManyList = new ArrayList<>();
		//小组当月应收金额
		Map<String, Double> nurseFee = getNurseFee(map);
		List<NurseGroupPercentagesDto> percentagesList = percentDao.percentagesList(map);
		
		//所有小组岗位薪资总和
		List<GroupPostSubsidy> subsidyCountByGroup = percentDao.getPostSubsidyCountByGroup();
		 Map<String, Double> collect = subsidyCountByGroup.stream().collect(Collectors.toMap(GroupPostSubsidy::getGroupId,GroupPostSubsidy::getPostSubsidyCount));
		//每个护工的实出勤天数
		Map<String, Long> attendMap = workService.queryAllByStartAndEndTime(startTime, endTime);
		Map<String,String> groupMap1 = new HashMap<>();
		groupMap1.put("hospitalId", courrentUser.getHospitalId());
		List<NurseGroupPercentagesDto> percentagesList1 = percentDao.percentagesList(groupMap1);
		//每个小组参与提成后实出勤天数
		Map<String, Integer> groupActualDays = getGroupActualDays(map,percentagesList1);
		
		//临时护工对应的基本薪资 key： 小组Id value：基本薪资
		Map<String,Double> tempNurseFee = new HashMap<>();
		//所有临时护工信息
		List<TemporaryNurseDto> temporaryNurseInfo = percentDao.temporaryNurseInfo();
		for (TemporaryNurseDto temporaryNurseDto : temporaryNurseInfo) {
			//应出勤天数
			int shouldAttendDay = nowMonthDays - temporaryNurseDto.getVacationDay();
			//该护工在改小组出勤天数
			Integer day = workService.queryByWorkIdAndGroupId(temporaryNurseDto.getNurseId(), temporaryNurseDto.getGroupId(), startTime, endTime);
			Double tempNurseFees = temporaryNurseDto.getBaseSalary()/shouldAttendDay*day;
			tempNurseFee.put(temporaryNurseDto.getGroupId(), tempNurseFees);
		}
		
		for (NurseGroupPercentagesDto nurseGroupPercentagesDto : percentagesList) {
			ManyToManyPercentagesDto manyDto = new ManyToManyPercentagesDto();
			manyDto.setNurseId(nurseGroupPercentagesDto.getNurseId());
			manyDto.setWorkerName(nurseGroupPercentagesDto.getWorkerName());
			manyDto.setGroupId(nurseGroupPercentagesDto.getGroupId());
			manyDto.setGroupName(nurseGroupPercentagesDto.getGroupName());
			manyDto.setBankCard(nurseGroupPercentagesDto.getBankCard());
			manyDto.setBankFullName(nurseGroupPercentagesDto.getBankFullName());
			manyDto.setBankName(nurseGroupPercentagesDto.getBankName());
			//岗位补贴
			manyDto.setPostSubsidy(Double.valueOf(String.format("%.2f",nurseGroupPercentagesDto.getPostSubsidy())));
			//应出勤天数=当月天数-休假天数
			int shouldAttendance = nowMonthDays-nurseGroupPercentagesDto.getVacationDay();
			//实出勤天数
			int actualAttendance = 0;
			if(!attendMap.isEmpty()){
				if(attendMap.get(nurseGroupPercentagesDto.getNurseId()) != null){
					actualAttendance = new Long(attendMap.get(nurseGroupPercentagesDto.getNurseId())).intValue(); 
					
				}
			}
			
			
			//小组护理费提成
			double nurseFeePercent = 0.00;
			//改护工所有待过的小组
			List<NurseGroupInfo> nurseGroupInfo = percentDao.nurseGroupInfo(nurseGroupPercentagesDto.getNurseId());
			for (NurseGroupInfo nurseGroupInfo2 : nurseGroupInfo) {
				
				if(NurseFeeTypeEnum._1.getKey().equals(nurseGroupInfo2.getNurseFeeType())){
					//固定底薪模型  ：护理费提成为 0
					manyDto.setNurseFeePercentages(nurseFeePercent);
				}else{
					//参与提成后在该小组的实出勤天数
					int attendanceDay = 0;
					Date participationDate = null;
					Date startTime2 = null;
					
					//是否参与提成  1：是  0：否
					if("1".equals(nurseGroupPercentagesDto.getHavePercentages()) && "1".equals(nurseGroupPercentagesDto.getTemporaryNurse())){
						participationDate = TimeUtils.getDateByString(nurseGroupPercentagesDto.getParticipationDate(), TimeType.yyyy_MM_dd_HH_mm_ss);
						startTime2 = TimeUtils.getDateByString(map.get("startTime"), TimeType.yyyy_MM_dd);
						//判断开始时间是否大于开始参与提成时间
						if(participationDate.compareTo(startTime2)<0){
							attendanceDay = workService.queryByWorkIdAndGroupId(nurseGroupPercentagesDto.getNurseId(),nurseGroupInfo2.getGroupId(),map.get("startTime"), map.get("endTime")).intValue();
						}else{
							String stringByDate = TimeUtils.getStringByDate(participationDate, TimeType.yyyy_MM_dd);
							attendanceDay = workService.queryByWorkIdAndGroupId(nurseGroupPercentagesDto.getNurseId(),nurseGroupInfo2.getGroupId(),stringByDate, map.get("endTime")).intValue();
						}
						Integer groupDays = groupActualDays.get(nurseGroupInfo2.getGroupId());
						if(StringUtils.isEmpty(groupDays)){
							groupDays = 0;
						}
//						
//						int groupDays = 0;
//						if(participationDate.compareTo(startTime1)<0){
//							if(!StringUtils.isEmpty(nurseGroupPercentagesDto.getGroupId())){
//								groupDays = workService.queryByGroupId(Integer.parseInt(nurseGroupInfo2.getGroupId()),map.get("startTime"), map.get("endTime")).intValue();
//								
//							}
//						}else{
//							String stringByDate = TimeUtils.getStringByDate(participationDate, TimeType.yyyy_MM_dd);
//							if(!StringUtils.isEmpty(nurseGroupPercentagesDto.getGroupId())){
//								groupDays = workService.queryByGroupId(Integer.parseInt(nurseGroupInfo2.getGroupId()),stringByDate, map.get("endTime")).intValue();
//								
//							}
//						}
						//当前小组应收金额
						Double nurseFees = nurseFee.get(nurseGroupInfo2.getGroupId());
						if(nurseFees == null){
							nurseFees = 0.00;
						}
						
						if(groupDays != 0){
							Double postSubsidy = 0.00;
							if(!collect.isEmpty() && collect.size() != 0){
								if(!StringUtils.isEmpty(collect.get(nurseGroupInfo2.getGroupId()))){
									postSubsidy = collect.get(nurseGroupInfo2.getGroupId());
									
								}
								
							}
							//临时护工工资
							Double tempFee = tempNurseFee.get(nurseGroupInfo2.getGroupId());
							if(StringUtils.isEmpty(tempFee)){
								tempFee = 0.00;
							}
							
							//小组护理费提成 = （当月应收金额*提成比例 - 所有人的岗位工资-临时护工工资）/小组所有成员实出勤天数  
							nurseFeePercent += (nurseFees*nurseGroupInfo2.getProportions()/100-postSubsidy-tempFee)/groupDays*attendanceDay;
							
						}
						

					}else{
						//未参与提成后个人护理费提成
						nurseFeePercent += 0.00;
					}
				}
			}
			//参与提成后个人护理费提成
			manyDto.setNurseFeePercentages(Double.valueOf(String.format("%.2f",nurseFeePercent)));
			//计算得出的基本薪资=基本薪资/应出勤天数*实出勤天数
			double computeBaseSalary = nurseGroupPercentagesDto.getBaseSalary()/shouldAttendance*actualAttendance;
			double baseSalary = 0.00;
			if(NurseFeeTypeEnum._1.getKey().equals(nurseGroupPercentagesDto.getNurseFeeType())){
				
				//固定底薪模型  基本薪资为实际计算结果
				baseSalary =  computeBaseSalary ;
				//固定薪资无保底薪资  显示为0
				manyDto.setPayroll(0.00);
				
			}else{
				//保底薪资
				manyDto.setPayroll(Double.valueOf(String.format("%.2f",nurseGroupPercentagesDto.getPayroll())));
				//提成比例模型 若实际出勤天数 > 应出勤天数，基本薪资为基本薪资
				baseSalary = actualAttendance > shouldAttendance ?  nurseGroupPercentagesDto.getBaseSalary():computeBaseSalary;
			}
			
			//实出勤天数
			manyDto.setAttendanceDay(actualAttendance);
			//基本薪资
			manyDto.setBaseSalary(Double.valueOf(String.format("%.2f",baseSalary)));
			//岗位补贴
			manyDto.setPostSubsidy(Double.valueOf(String.format("%.2f",nurseGroupPercentagesDto.getPostSubsidy())));
			//小组护理费提成
//			Map<String, Double> nurseFeeMap = getNurseFeePercentages(nurseGroupPercentagesDto,map);
			
			
		
			//应提护理费 = 基本薪资+护理费提成+岗位补贴 
			double shouldPercentFee = baseSalary + manyDto.getNurseFeePercentages() + nurseGroupPercentagesDto.getPostSubsidy();
			manyDto.setShouldPercentFee(Double.valueOf(String.format("%.2f",shouldPercentFee)));
			//提成比例为无保底薪资
//			if("0".equals(nurseGroupPercentagesDto.getHavePayroll())){
//				//实提护理费金额 = 应提护理费金额
//				manyDto.setRealPercentFee(Double.valueOf(df.format(shouldPercentFee)));
//			}else{
			//实提护理费金额 = 应提护理费金额  >= 保底薪资 ？ 应提护理费金额 ： 保底/应出勤*实出勤+岗位补贴
			double percentFee = nurseGroupPercentagesDto.getPayroll()/shouldAttendance*actualAttendance + manyDto.getPostSubsidy();
			double realPercentFee = manyDto.getShouldPercentFee() >= manyDto.getPayroll() ? manyDto.getShouldPercentFee() : percentFee;
			manyDto.setRealPercentFee(Double.valueOf(String.format("%.2f",realPercentFee)));
//			}
			//将多对多字段封装
			manyToManyList.add(manyDto);
		}
		return manyToManyList;
	}

}
